﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using Utility;
using System.Data.SqlClient;
using System.Data;
namespace Manager
{
    /// <summary>
    /// HW_Org业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_OrgManager
    {
        			
        #region##通过主键ID得到HW_Org
        /// <summary>
        /// 功能：通过ID得到HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_Org GetById(string orgcode)
        {            
            using (HW_OrgService ws = new HW_OrgService())
			{
				HW_Org org= ws.GetById(orgcode);
                HW_OrgDetailService service=new HW_OrgDetailService ();
                org.hw_OrgDetail = service.GetById(orgcode);
                return org;
			}
        }
        #endregion
		
		#region##通过主键ID删除HW_Org
        /// <summary>
        /// 功能：通过主键ID删除HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string orgcode)
		{
            HW_XXX_UserService xxxuserService = new HW_XXX_UserService();
            HW_OrgUserService orguserService = new HW_OrgUserService();
            //HW_School_UserService schooluserService = new HW_School_UserService();
            //HW_SchoolService schoolService = new HW_SchoolService();
            var orgusers= orguserService.GetByOrgCode(orgcode).ToList();
            HW_OrgDetailService detailService = new HW_OrgDetailService();
            SqlConnection conn=new SqlConnection (Utility.WebConn.connString);
            if(conn.State==System.Data.ConnectionState.Closed)
                conn.Open();
            SqlTransaction trans=conn.BeginTransaction();
            try
            {
                using (HW_OrgService ws = new HW_OrgService())
                {
                    foreach (var orguser in orgusers)
                    {
                        orguserService.DeleteById(trans, orguser.XX_UserName);
                        xxxuserService.DeleteById(trans, orguser.XX_UserName);
                    }
                    detailService.DeleteById(trans, orgcode);
                    int n = ws.DeleteById(trans, orgcode);
                    trans.Commit();
                    return n;
                }
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                conn.Close();
            }
		}
		#endregion			
		
        #region##添加HW_Org
        /// <summary>
        /// 功能：添加HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_Org hw_org)
        {
            using (HW_OrgService ws = new HW_OrgService())
			{
                HW_OrgDetailService service = new HW_OrgDetailService();
                SqlConnection conn = new SqlConnection(WebConn.connString);
                SqlTransaction trans=conn.BeginTransaction();
                try
                {
                    int n=ws.Add(hw_org);
                    service.Add(hw_org.hw_OrgDetail);
                    trans.Commit();
                    return n;
                }
                catch
                {
                    trans.Rollback();
                    return 0;
                }
                //int n=ws.Add(hw_org);
                //service.Add(hw_org.hw_OrgDetail);
                //return n;
			}
        }
        /// <summary>
        /// 功能：添加HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_Org hw_org,HW_XXX_User xxxUser,HW_OrgUser orguser)
        {
            using (HW_OrgService ws = new HW_OrgService())
            {
                HW_OrgDetailService service = new HW_OrgDetailService();

                HW_XXX_UserService xxxUserService = new HW_XXX_UserService();
                HW_OrgUserService orgUserService = new HW_OrgUserService();
                SqlConnection conn = new SqlConnection(WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    ws.Add(trans,hw_org);
                    service.Add(trans,hw_org.hw_OrgDetail);
                    xxxUserService.Add(trans,xxxUser);
                    int n = orgUserService.Add(trans,orguser);
                    trans.Commit();
                    return n;
                }
                catch(Exception e)
                {
                    trans.Rollback();
                    throw e;
                    //return 0;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
		
		#region##得到HW_Org集合
		/// <summary>
        /// 功能：得到HW_Org集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_Org> GetList()
		{
		    using (HW_OrgService ws = new HW_OrgService())
			{
				return ws.GetList();
			}
		}
        public DataTable GetSubList(int pageIndex, int pageSize, out int count,string pdistrictCode)
        {
            using (HW_OrgService ws = new HW_OrgService())
            {
                return ws.GetSubList(pageIndex, pageSize,out count, pdistrictCode);
            }
        }
        public DataTable Search(string district, string searchString)
        {
            using (HW_OrgService ws = new HW_OrgService())
            {
                return ws.Search(district, searchString);
            }
        }
		#endregion
		
		#region##修改HW_Org
        /// <summary>
        /// 功能：修改HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_Org(HW_Org hw_org)
        {
            using (HW_OrgService ws = new HW_OrgService())
			{
				int n= ws.ModHW_Org(hw_org);
                HW_OrgDetailService service = new HW_OrgDetailService();
                service.ModHW_OrgDetail(hw_org.hw_OrgDetail);
                return n;
			}
        }
        #endregion
		
		#region##统计HW_Org
		/// <summary>
        /// 修改HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_OrgService ws = new HW_OrgService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_Org
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_OrgService ws = new HW_OrgService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_Org
        public int ResetPassWord(string xx_username, string pass)
        {
            using (SqlConnection conn = new SqlConnection(Utility.WebConn.connString))
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_OrgUserService ws = new HW_OrgUserService();
                    HW_XXX_UserService service = new HW_XXX_UserService();
                    service.ResetPassword(trans, xx_username, pass);
                    //string pass=System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    int n = ws.ResetPassword(trans, xx_username, pass);
                    trans.Commit();
                    return n;

                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        #endregion
    }
}